package org.dromara.visitor.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.dromara.common.core.domain.R;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.visitor.domain.dto.*;
import org.dromara.visitor.service.PhoneBookService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 通讯录
 */
@RestController
@RequestMapping("/phoneBook")
public class PhoneBookController {
    @Resource
    private PhoneBookService phoneBookService;

    /**
     * 新增
     * @param addReq
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody PhoneBookAddReq addReq){
        phoneBookService.add(addReq);
        return R.ok();
    }

    /**
     * 下载导入模板
     * @param response
     */
    @GetMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response) {
        String templatePath = "templates/vis_phonebook.xlsx";
        // 下载文件名
        String fileName = "通讯录导入模板.xlsx";
        ExcelUtil.downloadTemplate(response, templatePath, fileName);
    }

    /**
     * 设置审批层级
     * @param req
     * @return
     */
    @PostMapping("/setApproveLevel")
    public R setApproveLevel(@RequestBody PhoneBookBatchReq req){
        phoneBookService.setApproveLevel(req.getId(), req.getApproveId());
        return R.ok();
    }

    /**
     * 批量设置审批层级
     * @param req
     * @return
     */
    @PostMapping("/setBatchApproveLevel")
    public R setBatchApproveLevel(@RequestBody PhoneBookBatchReq req){
        phoneBookService.setBatchApproveLevel(req.getIds(), req.getApproveId());
        return R.ok();
    }

    /**
     * 删除接口
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public R delete(@RequestParam("id") Long id){
        phoneBookService.removeById(id);
        return R.ok();
    }

    /**
     * 列表接口
     * @param queryReq
     * @return
     */
    @PostMapping("/listPage")
    public R<Page<PhoneBookResp>> listPage(@RequestBody PhoneBookQueryReq queryReq){
        Page<PhoneBookResp> page = phoneBookService.listPage(queryReq);
        return R.ok(page);
    }

    /**
     * 导入
     * @param file
     * @return
     */
    @PostMapping("/import")
    public R importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        List<PhoneBookImportExcel> list = ExcelUtil.importExcel(file.getInputStream(), PhoneBookImportExcel.class);
        phoneBookService.importExcel(list);
        return R.ok();
    }


}
